package cn.hyh.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * @auther :hyh
 * @desc :
 * @date :2020/1/6
 */
public class WebXmlConfig  implements WebApplicationInitializer {


	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		System.out.println("——————————————————————————————————————————————————");
		AnnotationConfigWebApplicationContext rootWac = new AnnotationConfigWebApplicationContext();
		rootWac.register(SpringParentConfig.class);
		System.out.println("开始注册ServletContextLoader............");
		 servletContext.addListener(new ContextLoaderListener(rootWac));

		AnnotationConfigWebApplicationContext mvcWac = new AnnotationConfigWebApplicationContext();
		 mvcWac.register(MvcChildConfig.class);
		System.out.println("开始注册 dispatcherServlet......");
		ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcWac));
		dispatcher.addMapping("/");
		//servlet 默认是在第一次访问时创建对象，通过 <load-on-startUp> 来设置在启动时初始化。
		dispatcher.setLoadOnStartup(1);
	}
}
